﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
using Model;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Text.RegularExpressions;

namespace NCRE学生考试端V1._0
{
    public class WindowsFindStartFiles
    {
        private readonly WinQuestionEntityBll winquestionbll = new WinQuestionEntityBll();

        #region"开头查找文件-韩梦甜-2015-11-23" 修改人：李金颖-更换方法-2018-12-5 15:55:47
        /// <summary>
        /// 开头查找文件
        /// </summary>
        /// <param name="winquestion"></param>
        public void FindStartFiles(WinQuestionEntity winquestion)
        {
            #region 修改人：李金颖-注释该方法-2018-12-5 15:54:16
            ////将正确答案，分值取出来，传给studentRecord

            //winquestion.questionFlag = "开头查找文件";

            //DataTable winQuestionDt = winquestionbll.LoadWindowsByFlag(winquestion);



            ////循环遍历正确答案
            //for (int i = 0; i < winQuestionDt.Rows.Count; i++)
            //{
            //    WinQuestionRecordEntity studentRecord = new WinQuestionRecordEntity();
            //    studentRecord.studentID = FrmLogin.studentID;
            //    studentRecord.timeStamp = MyInfo.TimeTamp();

            //    string correctAnswer;
            //    string fraction;
            //    StringBuilder examAnswer = new StringBuilder();
            //    string questionContent;
            //    //将考生ID传到studentRecord实体
            //    studentRecord.studentID = FrmLogin.studentID;
            //    //将试题的ID取出来
            //    studentRecord.questionID = Convert.ToDouble(winQuestionDt.Rows[i]["questionID"]);
            //    //将题的分数取出来
            //    fraction = winQuestionDt.Rows[i]["fraction"].ToString();
            //    //将正确答案取出来
            //    correctAnswer = winQuestionDt.Rows[i]["correctAnswer"].ToString();

            //    questionContent = winQuestionDt.Rows[i]["questionContent"].ToString();


            //    Regex re = new Regex("(?<=“).*?(?=”)", RegexOptions.None);
            //    MatchCollection mc = re.Matches(questionContent);
            //    string paperType;
            //    paperType = MyInfo.MyPaperType();
            //    string str = @"D:\" + MyInfo.MystudentID() + @"\计算机一级考生文件\" + paperType + "卷" + @"\winkt" + mc[1].ToString();
            //    //容灾处理，如果查不到路径，则为0分-韩梦甜-2014-12-6
            //    if (Directory.Exists(str) == false)
            //    {
            //        studentRecord.fraction = 0;
            //        studentRecord.examAnswer = "路径下无符合条件的文件";
            //    }
            //    else
            //    {
            //        var files = Directory.GetFiles(str, mc[0].ToString() + "*");

            //        //获取考生答案
            //        foreach (var file in files)
            //        {
            //            examAnswer = examAnswer.Append(files);
            //            studentRecord.examAnswer = examAnswer.ToString();
            //        }

            //        //判断答案是否正确
            //        if (examAnswer.ToString() == correctAnswer)
            //        {
            //            //加分
            //            studentRecord.fraction = Convert.ToDouble(fraction);

            //        }
            //        else
            //        {
            //            //不加分
            //            studentRecord.fraction = 0;
            //            studentRecord.examAnswer = "路径下无符合条件的文件";
            //        }
            //    }
            //    WindowsLoadInfo.list.Add(studentRecord);
            //}
            #endregion

            //将正确答案，分值取出来，传给studentRecord


            winquestion.questionFlag = "开头查找文件";

            DataTable winQuestionDt = winquestionbll.LoadWindowsByFlag(winquestion);


            //循环遍历正确答案
            for (int i = 0; i < winQuestionDt.Rows.Count; i++)
            {
                WinQuestionRecordEntity studentRecord = new WinQuestionRecordEntity();

                studentRecord.studentID = FrmLogin.studentID;
                studentRecord.timeStamp = MyInfo.TimeTamp();
                string correctAnswer;
                string fraction;
                string examAnswer;
                //将考生ID传到studentRecord实体
                studentRecord.studentID = FrmLogin.studentID;
                //将试题的ID取出来
                studentRecord.questionID = Convert.ToDouble(winQuestionDt.Rows[i]["questionID"]);
                //将题的分数取出来
                fraction = winQuestionDt.Rows[i]["fraction"].ToString();
                //将正确答案取出来
                correctAnswer = winQuestionDt.Rows[i]["correctAnswer"].ToString();
                string paperType;
                paperType = MyInfo.MyPaperType();

                string str = @"D:\" + MyInfo.MystudentID() + @"\计算机一级考生文件\" + paperType + "卷" + @"\winkt" + correctAnswer;
                if (File.Exists(str))
                {
                    //加分
                    studentRecord.fraction = Convert.ToDouble(fraction);
                    examAnswer = correctAnswer;
                    studentRecord.examAnswer = examAnswer;
                }
                else
                {
                    //不加分
                    studentRecord.fraction = 0;
                    studentRecord.examAnswer = "未找到文件";
                }
                WindowsLoadInfo.list.Add(studentRecord);
            }
        }
        #endregion
    }
}
